package cn.shyouchi.wuguanjia.android.ui.patrol.statistic;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.patrol.PatrolApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import cn.shyouchi.wuguanjia.android.widget.dialog.DatePickerDialogFragment;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PatrolStatisticActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R.id.sel_line)
    LinearLayout mSelLine;
    @BindView(R.id.rv_statistic)
    RecyclerView mRvStatistic;

    PatrolStatisticAdapter mPatrolStatisticAdapter;

    List<PatrolStatisticEntity.ResultMessageBean> mPatrolStatisticEntities = new ArrayList<>();

    PatrolApi mPatrolApi;


    String userid;
    String date;


    @Override
    public int initContentView() {
        return R.layout.activity_patrol_statistic;
    }

    @Override
    public void initInjector() {
        mPatrolApi = new PatrolApi(mContext);
        userid = getIntent().getStringExtra("userid");
        date = getIntent().getStringExtra("date");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        mPatrolStatisticAdapter = new PatrolStatisticAdapter(mPatrolStatisticEntities);

        mRvStatistic.setAdapter(mPatrolStatisticAdapter);
        mRvStatistic.setLayoutManager(new LinearLayoutManager(mContext));

        if (!TextUtils.isEmpty(userid)) {

            String[] date = this.date.split("-");

            mSelLine.setVisibility(View.GONE);

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, Integer.parseInt(date[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            String startTime = dateFormat.format(calendar.getTime());


            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            String endTime = dateFormat.format(calendar.getTime());


            fetchData(startTime, endTime);
        }


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick({R.id.tv_start_time, R.id.tv_end_time})
    public void onClick(View view) {
        showWhitDate(view.getId());
    }

    public void showWhitDate(final int id) {
        final DatePickerDialogFragment datePickerDialogFragment = new DatePickerDialogFragment(getSupportFragmentManager(), "选择日期", "date");
        datePickerDialogFragment.setOnDateDissListener(new DatePickerDialogFragment.onTimePickedListener() {
            @Override
            public void onPicked(String date) {
                switch (id) {
                    case R.id.tv_start_time:
                        mTvStartTime.setText(date);
                        break;
                    case R.id.tv_end_time:
                        mTvEndTime.setText(date);
                        break;
                }
                fetchData();
            }
        });
        datePickerDialogFragment.show(getSupportFragmentManager(), "date");
    }


    private void fetchData() {
        if (TextUtils.isEmpty(mTvStartTime.getText())) {
            ToastUtil.showToast("请选择开始日期");
            return;
        }
        if (TextUtils.isEmpty(mTvEndTime.getText())) {
            ToastUtil.showToast("请选择结束日期");
            return;
        }

        fetchData(mTvStartTime.getText().toString(), mTvEndTime.getText().toString());

    }


    private void fetchData(String startTime, String endTime) {
        mPatrolApi.getStatisticsList(userid, startTime, endTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<PatrolStatisticEntity>(mContext) {
                    @Override
                    public void onNext(PatrolStatisticEntity entity) {
                        mPatrolStatisticEntities.clear();
                        mPatrolStatisticEntities.addAll(entity.getResultMessage());
                        mPatrolStatisticAdapter.notifyDataSetChanged();
                    }
                });
    }


}
